# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base



  protect_from_forgery

  before_filter :initialize_user

  
  # make these available as ActionView helper methods.
  helper_method :logged_in?, :admin?
  

  protected

  # Check if the user is already logged in
  def logged_in?
    @current_user.is_a?(User)
  end

 def admin?
     logged_in? && @current_user.user_admin?
  end

  

  # setup user info on each page
  def initialize_user
    @current_user = User.find_by_id(session[:user]) if session[:user]
  end

  
  def admin_required
    redirect_to_login unless admin?
  end

  def login_required
    redirect_to_login unless logged_in?
  end

  def redirect_to_login
    redirect_to(:controller => 'sessions', :action => 'new')
  end
end
